package com.bw.main.navtab

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import com.bw.main.R
import me.majiajie.pagerbottomtabstrip.internal.RoundMessageView
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem

class NavTabItem : BaseTabItem {

    private lateinit var mIcon: ImageView
    private lateinit var mTitle: TextView
    private lateinit var mMsgView: RoundMessageView

    private lateinit var mDefaultIcon: Drawable
    private lateinit var mCheckedIcon: Drawable
    private lateinit var mTabTitle: String
    private var mChecked: Boolean = false
    private var mMsgNum: Int = 0

    private var mDefaultTextColor: Int = Color.parseColor("#9CA3AF")
    private var mCheckedTextColor: Int = Color.parseColor("#4A90E2")


    constructor(context: Context) : super(context) {
        initView(context)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initView(context)
    }

    /**
     * 初始化操作
     */
    private fun initView(context: Context) {
        //加载布局到当前组件
        LayoutInflater.from(context).inflate(R.layout.tab_bottom_item, this, true)
        mIcon = findViewById(R.id.iv_icon)
        mTitle = findViewById(R.id.tv_text)
        mMsgView = findViewById(R.id.riv)


    }

    fun initData(
        @DrawableRes defaultIcon: Int,
        @DrawableRes checkIcon: Int,
        title: String,
        tabCheck: Boolean,
        msgNum: Int
    ) {

        mDefaultIcon = ContextCompat.getDrawable(context, defaultIcon)!!
        mCheckedIcon = ContextCompat.getDrawable(context, checkIcon)!!
        mTabTitle = title
        mChecked = tabCheck
        mMsgNum = msgNum

        mTitle.text = mTabTitle
    }


    override fun setChecked(checked: Boolean) {

        if(checked){
            mIcon.setImageDrawable(mCheckedIcon)
            mTitle.setTextColor(mCheckedTextColor)
        }else{
            mIcon.setImageDrawable(mDefaultIcon)
            mTitle.setTextColor(mDefaultTextColor)
        }
        mChecked = checked


    }

    override fun setMessageNumber(number: Int) {
        mMsgView.messageNumber = number
        mMsgNum = number
    }

    override fun setHasMessage(hasMessage: Boolean) {
        mMsgView.setHasMessage(hasMessage)
    }

    override fun setTitle(title: String) {
        mTitle.text = title
        mTabTitle = title
    }

    override fun setDefaultDrawable(drawable: Drawable) {
        mDefaultIcon = drawable
        if(!mChecked){
            mIcon.setImageDrawable(drawable)
        }
    }

    override fun setSelectedDrawable(drawable: Drawable) {
        mCheckedIcon = drawable
        if(!mChecked){
            mIcon.setImageDrawable(drawable)
        }
    }

    override fun getTitle(): String {
        return mTabTitle
    }


}